package com.cowcow.apartment.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cowcow.apartment.common.result.Result;
import com.cowcow.apartment.entity.SystemUser;
import com.cowcow.apartment.web.admin.mapper.SystemUserMapper;
import com.cowcow.apartment.web.admin.service.SystemUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cowcow.apartment.web.admin.vo.system.user.SystemUserItemVo;
import com.cowcow.apartment.web.admin.vo.system.user.SystemUserQueryVo;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    @Override
    public IPage<SystemUserItemVo> pageSystemUserByQuery(IPage<SystemUser> page, SystemUserQueryVo queryVo) {
        return systemUserMapper.pageSystemUserByQuery(page, queryVo);
    }

    @Override
    public void registerUser(SystemUser systemUser) {
        // 1. 参数校验
        if (systemUser.getUsername() == null
                || systemUser.getUsername().isEmpty()
                || systemUser.getPassword() == null || systemUser.getPassword() .isEmpty()) {
            throw new RuntimeException("用户名或密码为空");
        }

        // 2. 用户唯一性检查
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername, systemUser.getUsername());
        SystemUser existingUser = this.getOne(lambdaQueryWrapper);
        if (existingUser != null) {
            throw new RuntimeException("用户名已存在");
        }
        // 加密密码？
        systemUser.setPassword(DigestUtils.sha256Hex(systemUser.getPassword()));
        this.saveOrUpdate(systemUser);
    }
}




